package core.java;

import java.io.*;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * @Author SSK
 * @Date Created in 16:12 2019/8/22
 * @Description zip
 * @Version 1.0
 */
public class ZipStudy{
    public static void main ( String[] args) throws InterruptedException, IOException {
        //zip("E:\\zipp.zip","E:/zip/","utf-8");
        notZip("E:\\zipp.zip","E:\\","utf-8");
    }
    public static void zip(String zip,String baseFilePath,String charset) throws IOException {
        File zipFile = new File(zip);
        if (!zipFile.getParentFile().exists()) {
            zipFile.getParentFile().mkdirs();
        }
        ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile),Charset.forName(charset));
        File file = new File(baseFilePath);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(zipOutputStream);
        zip(bufferedOutputStream,"",file,zipOutputStream);
        bufferedOutputStream.close();
        zipOutputStream.close();
        file = null;
    }
    public static void zip(BufferedOutputStream out, String base, File file , ZipOutputStream zipFile) throws IOException {
         if (file.isDirectory()){
             File[] files = file.listFiles();
             if (files.length==0) {
                 if (base.length()!=0)
                 zipFile.putNextEntry(new ZipEntry(base+"/"));
             }else {
                 if (base.length()==0) {
                     for (int i = 0; i < files.length; i++) {
                         zip(out,files[i].getName(),files[i],zipFile);
                     }
                 } else {
                     for (int i = 0; i < files.length; i++) {
                         zip(out,base+"/"+files[i].getName(),files[i],zipFile);
                     }
                 }
             }

         }else {
             zipFile.putNextEntry(new ZipEntry(base));
             BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
             byte[] b = new byte[1024];
             int len = -1;
             while ((len=bufferedInputStream.read(b))!=-1){
                 out.write(b,0,len);
                 out.flush();
             }
             bufferedInputStream.close();
         }
    }
    public static void notZip(String zip, String baseFilePath, String charset) throws IOException {
        ZipFile zipFile = new ZipFile(zip, Charset.forName(charset));
        ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zip), Charset.forName(charset));
        ZipEntry zipEntry;
        while ((zipEntry=zipInputStream.getNextEntry())!=null){
            if (zipEntry.isDirectory()) {
                System.out.println(zipEntry.getName());
            }else {
                File file = new File(baseFilePath,zipEntry.getName());
                if (!file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
                BufferedInputStream inputStream = new BufferedInputStream(zipFile.getInputStream(zipEntry));
                byte[] b  = new byte[1024];
                int len = -1;
                while ((len = inputStream.read(b))!=-1){
                    bufferedOutputStream.write(b,0,len);
                    bufferedOutputStream.flush();
                }
                bufferedOutputStream.close();
                inputStream.close();
                fileOutputStream.close();
            }
        }

    }
}
